/**
 * \file TimeMeasure_Win32.cpp
 * \brief TimeMeasure declaration class
 * \author Edouard ROGE
 * \date 08 august 2012
 */

#include "TimeMeasure.h"

namespace Core
{
	namespace Time
	{

		TimeMeasure::TimeMeasure()
		{
			QueryPerformanceFrequency((LARGE_INTEGER *)&m_dwFrequency);
		}

		TimeMeasure::~TimeMeasure()
		{
			m_Measures.clear();
		}

		uint32 TimeMeasure::AddMeasure()
		{
			m_Mutex.Lock();
			m_Measures.resize(m_Measures.size() + 2);
			m_Mutex.Unlock();
			return m_Measures.size() / 2 - 1;
		}

		void TimeMeasure::Start(uint32 _iIndex)
		{
			m_Mutex.Lock();
			QueryPerformanceCounter((LARGE_INTEGER *)&(m_Measures[_iIndex * 2]));
			m_Mutex.Unlock();
		}

		void TimeMeasure::Stop(uint32 _iIndex)
		{
			m_Mutex.Lock();
			QueryPerformanceCounter((LARGE_INTEGER *)&(m_Measures[_iIndex * 2 + 1]));
			m_Mutex.Unlock();
		}

		float TimeMeasure::GetMeasure(int32 _iIndex) const
		{
			return ((uint32)((m_Measures[2 * _iIndex + 1] - m_Measures[2 * _iIndex]) * 1000000 / m_dwFrequency)) / 1000.f;
		}
	}
}